﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web.Mvc;
using AFT.Parking.Bizlogic.Parkings;
using AFT.Parking.Foundation.Extensions;
using AFT.Parking.WebSite.Filter;
using Model=AFT.Parking.Entity;
using AFT.Parking.Bizlogic.Malls;
using AFT.Parking.Entity;
using AFT.Parking.Entity.ArgumentEntity;

namespace AFT.Parking.WebSite.Controllers
{
    [AdminOnly]
    public class MallLocationController : BaseController
    {
        private readonly MallLocationBiz _mallLocationBiz;
        private readonly ParkingBiz _parkingBiz;

        public MallLocationController()
        {
            _mallLocationBiz = new MallLocationBiz();
            _parkingBiz=new ParkingBiz();
        }

        [HttpGet]
        public ActionResult MallLocations(string name, int mallAreaId = 0, int page = 1)
        {
            Expression<Func<MallLocation, bool>> exp = u => true;
            if (!string.IsNullOrEmpty(name))
            {
                exp = u => u.Name == name;
            }
            if (mallAreaId > 0)
            {
                exp = u => u.MallAreaId == mallAreaId;
                ViewBag.MallAreaId = mallAreaId;
                ViewBag.MallAreaName = _parkingBiz.GetMallArea(mallAreaId).Name;
            }
            var p = new PaginationParam<MallLocation>
            {
                Expression = exp,
                PageSize = 20,
                PageIndex = page,
                OrderByList = new List<OrderBy<MallLocation>>
                {
                    new OrderBy<MallLocation> {Sort = false, exp = u => u.Id}
                }
            };
            return View(_mallLocationBiz.GetMallLocation(p));
        }

        [HttpGet]
        public ActionResult AddMallStores(int id)
        {
            var mallArea = _parkingBiz.GetMallArea(id);
            if (mallArea == null)
                return Redirect("/mallAreas/Floors");
            ViewBag.MallAreaName = mallArea.Name;
            return View(new MallLocation { MallAreaId = id,IsEnable = true});
        }

        [HttpPost]
        public ActionResult AddMallStores(MallLocation mallLocation)
        {
            var msg = string.Empty;
            var icon = "error";
            if (mallLocation.Name.IsNullOrEmpty())
                msg = "店铺名称不能为空；";
            if (mallLocation.LocationX <= 0 || mallLocation.LocationY <= 0)
                msg += "经纬度不能为空；";
            if (!_mallLocationBiz.ValidateNewLocation(mallLocation))
            {
                msg += "该地址存在营业中的店铺";
            }
            if (msg.Length == 0)
            {
                if (_mallLocationBiz.AddStore(mallLocation))
                {
                    icon = "success";
                    msg = "保存成功。";
                }
                else
                    msg = "保存失败，存在相同的店铺信息。";
            }
            
            ViewBag.Message = msg;
            ViewBag.Icon = icon;
            if (icon == "error")
            {
                return View(mallLocation);
            }
            return Redirect("/mallLocation/mallLocations?mallAreaId="+mallLocation.MallAreaId);
        }

        [HttpGet]
        public ActionResult EditMallLocation(int id)
        {
            var store = _mallLocationBiz.GetStoreInfo(id);
            if (store == null)
                return Redirect("/mallLocation/mallLocations");
            return View(store);
        }

        [HttpPost]
        public ActionResult EditMallLocation(MallLocation store)
        {
            var msg = string.Empty;
            var icon = "error";

            if (store.Name.IsNullOrEmpty())
                msg = "店铺名称不能为空；";
            if (store.LocationX <= 0 || store.LocationY <= 0)
                msg += "经纬度不能为空；";
            if (msg.Length == 0)
            {
                if (_mallLocationBiz.EditStore(store))
                {
                    icon = "success";
                    msg = "保存成功。";
                }
                else
                    msg = "保存失败，存在相同的停车场信息。";
            }
            ViewBag.Message = msg;
            ViewBag.Icon = icon;
            if (icon == "error")
            {
                return View(store);
            }
            return Redirect("/mallLocation/mallLocations?mallAreaId=" + store.MallAreaId);
        }
    }
}